package com.hznu.jspFinal.controller;


import com.hznu.jspFinal.entity.ExerciseInfo;
import com.hznu.jspFinal.myBeans.Exercise;
import com.hznu.jspFinal.myBeans.ExerciseMatch;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.ExerciseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.xmlbeans.impl.common.ValidatorListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * author: wp
 * time 21:30 2018/06/07
 */
@Api(description = "试题管理接口",value = "Exercise")
@RestController
@RequestMapping("/Exercise")
public class ExerciseController {

    @Autowired
    private ExerciseService exerciseService;


    @ApiOperation(value="新增试题")
    @PostMapping("/add")
    public R addOne(@RequestBody Exercise exercise)
    {
        try{
            exerciseService.addOne(exercise);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return  R.error();
        }

    }

    @ApiOperation(value = "修改试题")
    @PostMapping("/update")
    public R update(@RequestBody Exercise exercise)
    {
        try{
            exerciseService.update(exercise);
            return  R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value="通过题目情况表id删除试题")
    @GetMapping("/delete/{id}")
    public R deleteByExerciseInfoId(@PathVariable Integer id)
    {
        try{
            exerciseService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value="通过题目情况表id查找试题")
    @GetMapping("/findByExerinfoId/{id}")
    public R findByExerinfoId(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",exerciseService.findByExerinfoid(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "通过知识点id查找试题集")
    @GetMapping("/findByKpointId/{id}")
    public R findByKpointId(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",exerciseService.findByKpointId(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "通过课程id查找试题集")
    @GetMapping("/findByCourseId/{id}")
    public R findByCourseId(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",exerciseService.findByCourseId(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * time： 11:01 2018/06/14
     * @param exerciseMatch
     * @return
     */
    @ApiOperation(value = "通过关键词keyword[,courseid,kpointid]搜索试题集")
    @PostMapping("/findByKeyword")
    public R findByKeyword(@RequestBody ExerciseMatch exerciseMatch)
    {
        try{
            return R.ok().put("data",exerciseService.findByKeyword(exerciseMatch.getKeyword(),
                                                                    exerciseMatch.getCourseid(),
                                                                    exerciseMatch.getKpointid()));

        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
